/*
 * $Id: EJBInjectionPostProcessor.java 77 2011-02-07 06:11:38Z samaxes $
 *
 * Copyright 2011 samaxes.com
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.samaxes.stripes.ejb3;

import net.sourceforge.stripes.controller.DefaultObjectFactory;
import net.sourceforge.stripes.controller.ObjectFactory;
import net.sourceforge.stripes.controller.ObjectPostProcessor;
import net.sourceforge.stripes.util.Log;

/**
 * <p>
 * An implementation of {@link ObjectPostProcessor} that calls {@link EJBHelper#injectBeans(Object)} to inject
 * dependencies marked with {@link EJBBean} in every type of object created by Stripes (Action Beans, Interceptors, Type
 * Converters, Formatters, etc.).
 * </p>
 * 
 * <p>
 * To configure {@code EJBInjectionPostProcessor}, add the following initialization parameter to your Stripes filter
 * configuration in {@code web.xml}:
 * 
 * <pre>
 * {@code
 * <init-param>
 *     <param-name>Extension.Packages</param-name>
 *     <param-value>com.samaxes.stripes.business</param-value>
 * </init-param>
 * }
 * </pre>
 * 
 * </p>
 * 
 * @author Samuel Santos
 * @version $Revision$
 */
public class EJBInjectionPostProcessor implements ObjectPostProcessor<Object> {

    private static final Log LOG = Log.getInstance(EJBInjectionPostProcessor.class);

    /**
     * Empty implementation of {@link ObjectPostProcessor#setObjectFactory(DefaultObjectFactory)}.
     * 
     * @param factory an implementation of {@link ObjectFactory} that simply calls {@link Class#newInstance()} to obtain
     *        a new instance
     */
    public void setObjectFactory(DefaultObjectFactory factory) {
    }

    /**
     * Calls {@link EJBHelper#injectBeans(Object)} to inject dependencies marked with {@link EJBBean} into the object
     * before returning it.
     * 
     * @param object the object to have beans injected into
     */
    public Object postProcess(Object object) {
        LOG.debug("Running EJB dependency injection for instance of ", object.getClass().getSimpleName());

        EJBHelper.injectBeans(object);

        return object;
    }
}
